<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 品牌model层
 * @author Toby.Tu 2016-09-02
 */
class Goods_model extends My_Model{
    public $tab = 'goods';
    public $pk = 'id';
    /**
     * 获取全部
     * @author Toby.tu 2016-09-02
     */
    public function getAll($brand_id=0){
        if(empty($tab)) $tab = $this->getTab();
        $field = 'id,name,description,status,create_time,update_time';
        $query = $this->db->select($field)->where('delete_id',0);
        if($brand_id > 0){
            $query->where('brand_id',$brand_id);
        }
        $result = $query->order_by('update_time desc')->get($tab);
        if(!empty($result)){
            return $result->result_array();
        }
        return array();
    }
    /**
     * 获取用户列表
     * @author Toby.tu 2016-09-01
     */
    public function getGoodsList($search=array(),$current_page=1,$pagesize=0){
        if(isset($search['is_admin']) && 'yes' == $search['is_admin']){
            $field = 'goods.*,brand.name as brand_name';
            $query = $this->select($field)->join('brand','brand.id=goods.brand_id','left');
        }else{
            $field = '*';
            $query = $this->select($field);
        }
        $query->where('goods.delete_id',0);
        if(isset($search['name'])){//品牌故事名称
            $query->like('goods.name',$search['name']);
        }
        if(isset($search['brand_id']) && $search['brand_id'] > 0){//品牌
            $query->where('goods.brand_id',$search['brand_id']);
        }
        if(isset($search['status'])){//状态
            $query->where('goods.status',$search['status']);
        }
        if(isset($search['category_first'])){//一级分类
            $query->where('goods.category_first',$search['category_first']);
        }
        if(isset($search['category_parent'])){//二级分类
            $query->where('goods.category_parent',$search['category_parent']);
        }
        if(isset($search['category_id'])){//三级分类
            $query->where('goods.category_id',$search['category_id']);
        }
        $query->order_by('goods.id desc');
        if(empty($pagesize)){
            $pagesize = $this->pagesize;
        }
        $start = ($current_page-1)*$pagesize;
        $query->limit($pagesize,$start);
        $rows = $this->get();
        if(!empty($rows)){
            return $rows;
        }
        return array();
    }
    /**
     * 获取品牌故事总数
     * @author Toby.tu 2016-09-01
     */
    public function getGoodsCount($search=array()){
        $tab = $this->getTabName();
        $field = 'count('.$tab.'.id) as user_count';
        $query = $this->select($field)->where('delete_id',0);
        if(isset($search['name'])){//品牌故事名称
            $query->like('name',$search['name']);
        }
        if(isset($search['brand_id']) && $search['brand_id'] > 0){//品牌
            $query->where('brand_id',$search['brand_id']);
        }
        if(isset($search['status'])){//状态
            $query->where('status',$search['status']);
        }
        $query->order_by('update_time desc');
        $row = $this->getRow();
        if(!empty($row)){
            return $row['user_count'];
        }
        return 0;
    }
    /**
     * 获取品牌对应的商品
     * @author Toby.tu 2017-03-03
     */
    public function getgoodsByBrandId($brand_id=0){
        $field = '*';$tab = $this->getTabName();
        $query = $this->db->select($field)->where('delete_id',0)->where('status',1);
        if($brand_id > 0){
            $query->where('brand_id',$brand_id);
        }
        $result = $query->order_by('update_time desc')->get($tab);
        if(!empty($result)){
            return $result->result_array();
        }
        return array();
    }
    /**
     * 获取用户列表
     * @author Toby.tu 2016-09-01
     */
    public function getGoodsByCategory($search=array()){
        $field = '*';
        $query = $this->select($field);
        $query->where('delete_id',0)->where('status',1);
        if(isset($search['brand_id'])){
            $query->where('brand_id',$search['brand_id']);
        }
        if(isset($search['category_id'])){
            $query->where('category_id',$search['category_id']);
        }
        $query->order_by('update_time desc');
        $rows = $this->get();
        if(!empty($rows)){
            return $rows;
        }
        return array();
    }
}
